#coding: utf-8

from pylab import *
import sys

def draw_errbox(ax, x, y, width=1.0):
    w = width * 0.5
    wm = w*0.8


    ## YorkUrban
    # vmean = mean(y)
    # vmin = mean(y[5:8])
    # vb = mean(y[25:28])
    # vm = mean(y[50:53])
    # vt = mean(y[75:78])
    # vmax = mean(y[94:97])

    ## ApaSt
    vmean = mean(y)
    vmin = mean(y[0])
    vb = mean(y[12])
    vm = mean(y[24])
    vt = mean(y[36])
    # vmax = mean(y[47])
    vmax = mean(y[44])




    LL=[]

    LL.append(Line2D([vb, vt, vt, vb, vb],
                     [x-w, x-w, x+w, x+w, x-w], lw=2,color='#0000ff') )
    LL.append(Line2D([vmax, vmax],[x-wm, x+wm],lw=2,color='#0000ff') )
    LL.append(Line2D([vmin, vmin],[x-wm, x+wm],lw=2,color='#0000ff') )
    LL.append(Line2D([vt, vmax],[x, x], lw=2,ls='--',color='#0000ff') )
    LL.append(Line2D([vb, vmin],[x, x], lw=2,ls='--',color='#0000ff') )

    LL.append(Line2D([vm, vm],[x-w, x+w],lw=2,color='#ff0000') )
    LL.append(Line2D([vm],[x],color='#ff0000', mew=2, ms=7, marker='+') )
    LL.append(Line2D([vmean],[x],color='#ff0000', mew=2, ms=7, marker='x') )

    LL.append(Line2D(y[-3:],[x,x,x],
                     color='#000000', ls='', mew=1, ms=3, marker='x') )

    # LL.append(Line2D(y[97:],[x,x,x,x,x],
    #                  color='#000000', ls='', mew=1, ms=3, marker='x') )
    # LL.append(Line2D(y[:5],[x,x,x,x,x],
    #                  color='#000000', ls='', mew=1, ms=3, marker='x') )

    for ll in LL:
        ax.add_line(ll)





fname = sys.argv[1]
xx = loadtxt(fname)


if fname[:2] == 'it':
    sq = False
    it = int(fname[2:-4])

elif fname[:5] == 'sq_it':
    sq = True
    it = int(fname[5:-4])

elif fname[:6] == 'apa_it':
    sq = False
    apa = True
    it = int(fname[6:-4])


ion()

figure(1, figsize=(10,5))

if sq:
    suptitle(u'Avaliação de desempenho do Corisco - Erro quadrático, %d iterações do RANSAC'%(it))
elif apa==True:
    suptitle(u'Avaliação de desempenho do Corisco - Base ApaSt, %d iterações do RANSAC'%(it))
else:
    suptitle(u'Avaliação de desempenho do Corisco - Erro de Tukey, %d iterações do RANSAC'%(it))

ax = axes([0.07,0.1,0.6,0.8])

title(u'Distribuição do erro')

ax.set_yscale('log', basey=2)
#ax.set_xscale('log', basex=2)


Nd = 48


for n in range(xx.shape[0]):
    #x = 3*log(xx[n,0])/log(2)
    x = xx[n,0]
    data = xx[n, 1:Nd+1]
    draw_errbox(ax, x, data, width=0.6*x/3)

grid()

#plot([4,4], [1e-3,1e3], 'r-')

ylim(150,.8)


if not sq:
    xlim(0.0,25)
else:
    xlim(0.0,40)


yticks([1,2,4,8,16,32,64,128],[1,2,4,8,16,32,64,128])
ylabel(u'Largura da grade [pixels]')
xlabel(u'Erro [graus]')




###
###
bx=axes([0.69,0.1,0.3,0.8])
bx.set_yscale('log', basey=2)
bx.set_xscale('log', basex=10)

title(u'Duração do processo')

errorbar(xx[:,-2],xx[:,0], xerr=xx[:,-1], lw=2, color='r')
l1=plot(xx[:,-2],xx[:,0], 'ro-')[0]

#errorbar(xx[:,Nd+2],xx[:,0], xerr=xx[:,104], lw=2, color='b')
l2=plot(xx[:,-4],xx[:,0], 'bd--')[0]

legend([l1,l2], ['Total','RANSAC'], loc='lower right', numpoints=1)


grid()

yticks([1,2,4,8,16,32,64,128],[])


# xticks([.03,.1,.3,1,3,10,30,100,300],[.03,.1,.3,1,3,10,30,100,300])


xticks([0.1,1,10,100,1000],[0.1,1,10,100,1000])

if sq:
    xlim(.08, 10.0)
else:
    if it == 10000:
    #    xticks([5,10,20,40,80,160,320],[5,10,20,40,80,160,320])
        xlim(4, 500)
    elif it == 1000:
    #    xticks([.5,1,2,4,8,16,32],[.5,1,2,4,8,16,32])
        xlim(0.4, 50)
    elif it == 200:
        # xticks([.1,.2,.4,.8,1.6,3.2,6.4],[.1,.2,.4,.8,1.6,3.2,6.4])
    #    xticks([.5,1,2,4,8,16,32],[.5,1,2,4,8,16,32])
        xlim(.08, 10.0)

ylim(150,.8)

xlabel('Tempo [segundos]')


if apa:
    savefig('ava-apa%d.pdf'%(it))
else:
    savefig('ava%d.pdf'%(it))
